#!/bin/bash

# FluvioX测试脚本

set -e

echo "🧪 运行FluvioX测试套件..."

# 测试后端
echo "🦀 测试Rust后端..."
cd backend
cargo test
cd ..

# 测试前端
echo "⚛️ 测试React前端..."
cd frontend
npm test
cd ..

# API集成测试
echo "🔗 运行API集成测试..."

# 检查健康状态
echo "📊 检查API健康状态..."
response=$(curl -s http://localhost:8080/api/v1/health || echo "failed")
if [[ $response == *"healthy"* ]]; then
    echo "✅ API健康检查通过"
else
    echo "❌ API健康检查失败"
    exit 1
fi

# 测试连接器API
echo "🔌 测试连接器API..."
curl -s -X GET http://localhost:8080/api/v1/connectors > /dev/null
echo "✅ 连接器API测试通过"

# 测试连接API
echo "🔗 测试连接API..."
curl -s -X GET http://localhost:8080/api/v1/connections > /dev/null
echo "✅ 连接API测试通过"

# 测试主题API
echo "📊 测试主题API..."
curl -s -X GET http://localhost:8080/api/v1/topics > /dev/null
echo "✅ 主题API测试通过"

# 测试Fluvio连接
echo "🌊 测试Fluvio连接..."
if fluvio topic list > /dev/null 2>&1; then
    echo "✅ Fluvio连接测试通过"
else
    echo "❌ Fluvio连接测试失败"
    exit 1
fi

# 测试数据库连接
echo "🐘 测试数据库连接..."
if PGPASSWORD=password psql -h localhost -U postgres -d fluviox -c "SELECT 1;" > /dev/null 2>&1; then
    echo "✅ 数据库连接测试通过"
else
    echo "❌ 数据库连接测试失败"
    exit 1
fi

echo "🎉 所有测试通过！"
